<template>
  <div>
    <el-page-header @back="$goBack" :content="pageTitle" />
    <el-form :model="form" :rules="rules" ref="formRef">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="所属卡类别" prop="vip_id">
            <el-select v-model="form.vip_id" placeholder="请选择" style="width: 100%">
              <el-option
                v-for="vip in categories"
                :key="vip.id"
                :value="vip.id"
                :label="vip.title"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="卡名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入卡名称" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="售价" prop="price">
            <el-input v-model="form.price" placeholder="0.00" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="原价" prop="origin_price">
            <el-input v-model="form.origin_price" placeholder="0.00" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="有效期" prop="expire">
            <el-tooltip content="数字0代表永久有效，>0的数字代表天数">
              <el-input v-model="form.expire" placeholder="0" />
            </el-tooltip>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="使用说明" prop="usage_desc">
        <el-input v-model="form.usage_desc" type="textarea" placeholder="请输入使用说明" />
      </el-form-item>
      <el-form-item label="权益说明" prop="interests_desc">
        <el-input v-model="form.interests_desc" type="textarea" placeholder="请输入权益说明" />
      </el-form-item>
      <el-form-item label="购买说明" prop="buy_desc">
        <el-input v-model="form.buy_desc" type="textarea" placeholder="请输入购买说明" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :loading="loading" @click="submit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { VipActionsEnum, VipCardActionsEnum } from '../../../_/vue/store/vip'
export default {
  data () {
    return {
      pageTitle: '添加会员卡',
      loading: false,
      id: '',
      rules: {
        name: [{ required: true, message: '请输入卡名称', trigger: 'blur' }],
        price: [{ required: true, message: '请输入售价', trigger: 'blur' }],
        origin_price: [{ required: true, message: '请输入原价', trigger: 'blur' }],
        expire: [{ required: true, message: '请输入原价', trigger: 'blur' }],
        usage_desc: [{ required: true, message: '请输入使用说明', trigger: 'blur' }],
        interests_desc: [{ required: true, message: '请输入权益说明', trigger: 'blur' }],
        buy_desc: [{ required: true, message: '请输入购买说明', trigger: 'blur' }]
      },
      form: {
        name: '',
        price: '',
        origin_price: '',
        expire: '',
        usage_desc: '', // 使用说明
        interests_desc: '', // 权益说明
        buy_desc: '' // 购买说明
      },
      categories: []
    }
  },
  async mounted () {
    if (this.$route.query.id) {
      this.id = this.$route.query.id
      const resp = await this.edit(this.id)
      this.form = resp.data
      this.pageTitle = '编辑会员卡'
    }
    const resp = await this.vipSelect()
    this.categories = resp.data
  },
  methods: {
    ...mapActions({
      edit: VipCardActionsEnum.EDIT,
      save: VipCardActionsEnum.SAVE,
      store: VipCardActionsEnum.STORE,
      vipSelect: VipActionsEnum.SELECT
    }),
    submit () {
      this.$refs.formRef.validate(async (valid) => {
        if (valid) {
          this.loading = true
          if (this.id) {
            await this.save({ id: this.id, data: this.form })
          } else {
            await this.store(this.form)
          }
          this.$message.success(this.id ? '保存成功' : '新增成功')
          this.$goBack()
        }
        this.loading = false
        return false
      })
    }
  }
}
</script>
